//
//  MainViewController.swift
//  VB
//
//  Created by mac on 16/10/6.
//  Copyright © 2016年 华仔工作室. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupChildControllers()
    }

}


//MARK - 设置界面
extension MainViewController{
    
    ///设计所有子控制器
    func setupChildControllers() {
        let array = [
            ["clsName" : "HomeViewController","title" : "首页","imageName" : "home"],
            ["clsName" : "MessageViewController","title" : "消息","imageName" : "message_center"],
            ["clsName" : "DiscoverViewController","title" : "发现","imageName" : "discover"],
            ["clsName" : "ProfileViewController","title" : "我","imageName" : "profile"]
        ]
        
        var arrayM = [UIViewController]()
        for dict in array {
            arrayM.append(controller(dict: dict))
        }
        
        viewControllers = arrayM
        
    }
    
    ///使用字典创建一个子控制器
    private func controller(dict: [String: String]) -> UIViewController{
        
        guard let clsName = dict["clsName"],
        let title = dict["title"],
        let imageName = dict["imageName"],
        let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? UIViewController.Type
            
        else {
            return UIViewController()
        }
        
        let vc = cls.init()
        vc.title = title
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        let nav = NavigationController(rootViewController: vc)
        
        return nav
        
    }
}
